package com.logistics.modules.transportationManagement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.logistics.modules.transportationManagement.dao.ClientorderdetailsDao;
import com.logistics.modules.transportationManagement.entity.CargotypeEntity;
import com.logistics.modules.transportationManagement.entity.ClientorderdetailsEntity;
import com.logistics.modules.warehouseManagement.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;

import com.logistics.modules.transportationManagement.dao.CargoDao;
import com.logistics.modules.transportationManagement.entity.CargoEntity;
import com.logistics.modules.transportationManagement.service.CargoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 货物服务impl
 *
 * @author 水穷处云起时
 * @date 2022/06/03
 */
@Service("cargoService")
public class CargoServiceImpl extends ServiceImpl<CargoDao, CargoEntity> implements CargoService {

    @Autowired
    private CargoDao cargoDao;

    @Autowired
    private SeatService seatService;

    @Autowired
    private ClientorderdetailsDao clientorderdetailsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 转换参数
        Integer cargoType = Convert.toInt(params.get("cargoType"));
        String cargoName = Convert.toStr(params.get("cargoName"));
        // 实例化page对象
        IPage<CargoEntity> page = new Query<CargoEntity>().getPage(params);
        // 去数据库查询
        cargoDao.selectAll(page,cargoType,cargoName);
        return new PageUtils(page);
    }

    @Override
    public CargoEntity selectOneById(Integer id) {
        return cargoDao.selectOneById(id);
    }

    /**
     * 保存客户订单细节
     *
     * @param params 参数个数
     */
    @Override
    public void saveClientOrderDetails(Map<String, Object> params) {

        // 客户订单详情实体类
        ClientorderdetailsEntity clientorderdetailsEntity = new ClientorderdetailsEntity();
        // 货物实体类
        CargoEntity cargoEntity = new CargoEntity();
        // 把参数封装进货物实体类中
        BeanUtil.fillBeanWithMap(params, cargoEntity, false);
        // 把参数封装进客户订单类中
        BeanUtil.fillBeanWithMap(params, clientorderdetailsEntity, false);
        Integer clientOrderId = Convert.toInt(params.get("clientOrderId"));
        // 检查数据库是否有空余席位
        Boolean isThere = seatService.checkIfASeatIsVacant(cargoEntity, clientOrderId);
        if (!isThere) {
            throw new RuntimeException("仓库没有空闲席位!");
        }
        // 把货物保存到数据库中
        cargoDao.insert(cargoEntity);
        // 设置货物id
        clientorderdetailsEntity.setCargoId(cargoEntity.getId());
        // 保存到数据库
        clientorderdetailsDao.insert(clientorderdetailsEntity);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteCheck(List<Integer> asList) {
        // 根据货物类型的id去数据库惊醒匹配看看是否有货物使用该类型 有则无法删除
        int size = asList.size();

        for (int i = 0; i < size; i++) {
            int count = this.cargoDao.countByCargoTypeId(asList.get(i));
            if (count > 0) {
                throw new RuntimeException("您要删除的货物类型正在使用无法删除!");
            }
        }

    }


}